<?php

class ArticleController extends Controller{
	private $category;
	private $tags;
	private $model;
	public function __init(){
		$this->category = S('category');
		$this->tags = K('Tag')->getData();
		$this->model = K('Article');
	}
	public function index(){
		
		$data = K('ArticleCategoryView')->getData();
		foreach ($data as $k => $d) {
			$data[$k]['tags'] = K('Tag')->getTnameByaid($d['aid']);
		}
		$this->assign('data',$data);
		$this->display();
	}

	/*
	*	文章添加
	*/
	public function add(){
		if(IS_POST){
			//先插入文章表数据，返回主键 aid
			if($aid = $this->model->addArticle()){
				//aid tid 全获得了，向文章标签中间表中插入数据
				if(K('ArticleTags')->addData($aid)){
					$this->success('文章添加成功');
				}
			}
			$this->error('文章添加失败');
		}else{
			$this->assign('category',$this->category);
			$this->assign('tags',$this->tags);
			$this->display();
		}
	}
	/*
	*	文章编辑
	*/

	public function edit(){
		if(IS_POST){
			if($this->model->saveArticle()){
				$this->success('编辑成功','index');
			}else{
				$this->error('编辑失败');
			}

		}else{
			$aid = Q('aid',0,'intval');
			//根据aid 获得文章数据
			$data = $this->model->getDataByAid($aid);
			//获得当前文章 标签id
			$data['tids'] = K('ArticleTags')->getTid($aid);
			//分配文章数据
			$this->assign('data',$data);
			//栏目数据 
			$this->assign('category',$this->category);
			//标签数据
			$this->assign('tags',$this->tags);
			$this->display();
		}
	}
}

?>